# response to reviewer
suppressMessages(require(Seurat))
suppressMessages(require(ggplot2))
suppressMessages(require(cowplot))
suppressMessages(require(scater))
suppressMessages(require(scran))
suppressMessages(require(BiocParallel))
suppressMessages(require(BiocNeighbors))
library(data.table)
## 
## Attaching package: 'data.table'
## The following object is masked from 'package:SummarizedExperiment':
## 
##     shift
## The following object is masked from 'package:GenomicRanges':
## 
##     shift
## The following object is masked from 'package:IRanges':
## 
##     shift
## The following objects are masked from 'package:S4Vectors':
## 
##     first, second
setwd("~/covid19/result01/singlecell_data")

hms_individual_integrated<-readRDS(file="hms_individual_integrated_OK.rds")
p1 <- DimPlot(hms_individual_integrated, reduction = "umap", group.by = "celltype")
p1

hms_individual_integrated<- FindNeighbors(hms_individual_integrated, dims = 1:10)
## Computing nearest neighbor graph
## Computing SNN
hms_individual_integrated <- FindClusters( hms_individual_integrated, resolution = 0.5)
## Modularity Optimizer version 1.3.0 by Ludo Waltman and Nees Jan van Eck
## 
## Number of nodes: 52158
## Number of edges: 1604563
## 
## Running Louvain algorithm...
## Maximum modularity in 10 random starts: 0.8838
## Number of communities: 15
## Elapsed time: 20 seconds
pbmc <- RunTSNE(object = hms_individual_integrated)
DimPlot(object = pbmc, reduction = "tsne")

new.cluster.ids <- c("Macrophage", "Macrophage", "Macrophage", "Macrophage", "Neutrophils", "Macrophage", "Naive CD4+ T", "NK", "Neutrophils", "Dendritic", "T", "T","Basal","Plasma","T") 
names(new.cluster.ids) <- levels(pbmc)

hms_tsne <- RenameIdents(pbmc, new.cluster.ids)
DimPlot(hms_tsne, reduction = "tsne", label = TRUE, pt.size = 0.5)
## Warning: Using `as.character()` on a quosure is deprecated as of rlang 0.3.0.
## Please use `as_label()` or `as_name()` instead.
## This warning is displayed once per session.

saveRDS(hms_tsne, file = "hms_tsne.rds")

hms_tsne<-readRDS(file="hms_tsne.rds")

Macrophage<-subset(hms_tsne, idents=c('Macrophage'))
DimPlot(Macrophage, reduction = "tsne")

saveRDS(Macrophage, file="Macrophage_tsne.rds")

Neutrophils<-subset(hms_tsne, idents=c('Neutrophils'))
DimPlot(Neutrophils, reduction = "tsne")

saveRDS(Neutrophils, file="Neutrophils_tsne.rds")

Naive_CD4_T<-subset(hms_tsne, idents=c('Naive CD4+ T'))
DimPlot(Naive_CD4_T, reduction = "tsne")

saveRDS(Naive_CD4_T, file="Naive_CD4_T_tsne.rds")

NK<-subset(hms_tsne, idents=c('NK'))
DimPlot(NK, reduction = "tsne")

saveRDS(NK, file="NK_tsne.rds")

Dendritic<-subset(hms_tsne, idents=c('Dendritic'))
DimPlot(Dendritic, reduction = "tsne")

saveRDS(Dendritic, file="Dendritic_tsne.rds")

Basal<-subset(hms_tsne, idents=c('Basal'))
DimPlot(Basal, reduction = "tsne")

saveRDS(Basal, file="Basal_tsne.rds")

T<-subset(hms_tsne, idents=c('T'))
DimPlot(T, reduction = "tsne")

saveRDS(T, file="T_tsne.rds")

Plasma<-subset(hms_tsne, idents=c('Plasma'))
DimPlot(Plasma, reduction = "tsne")

saveRDS(Plasma, file="Plasma_tsne.rds")

DimPlot(Neutrophils, reduction = "tsne", split.by = "tech")

DimPlot(Basal, reduction = "tsne", split.by = "tech")

DimPlot(Dendritic, reduction = "tsne", split.by = "tech")

DimPlot(Macrophage, reduction = "tsne", split.by = "tech")

DimPlot(Naive_CD4_T, reduction = "tsne", split.by = "tech")

DimPlot(NK, reduction = "tsne", split.by = "tech")

DimPlot(Plasma, reduction = "tsne", split.by = "tech")

DimPlot(T, reduction = "tsne", split.by = "tech")